package com.zingrow.web.configure.enshrine.controller;

import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleIfStatement.Else;
import com.idaycrm.idaybase.utils.JsonUtils;
import com.zingrow.common.ReturnStandardDataFormat;
import com.zingrow.common.utils.EncodingUtils;
import com.zingrow.web.InformationDistribute.dao.InformationDistributeDao;
import com.zingrow.web.InformationDistribute.dao.impl.InformationDistributeDaoImpl;
import com.zingrow.web.base.controller.BaseController;
import com.zingrow.web.base.enums.OperationDeed;
import com.zingrow.web.base.enums.OperationModule;
import com.zingrow.web.configure.enshrine.model.Enshrine;
import com.zingrow.web.configure.enshrine.service.IEnshrineService;
import com.zingrow.web.configure.iformation.model.Iformation;
import com.zingrow.web.configure.iformation.service.IIformationService;
import com.zingrow.web.entity.InformationDistribute;
import com.zingrow.web.support.operate.service.IOptRecordService;
import com.zingrow.web.base.enums.OperationModule;
import com.zingrow.web.base.enums.OperationMinModule;
import com.zingrow.web.base.enums.OperationDeed;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = { "/enshrine/" }, produces = { "application/json;charset=UTF-8" })
public class EnshrineController extends BaseController {
	@Autowired
	private IEnshrineService enshrineService;

	@Autowired
	private IIformationService iformationService;

	@Autowired
	private IOptRecordService optRecordService;
	
	private InformationDistributeDao informationDistributeDao = new InformationDistributeDaoImpl();

	/**
	 * 收藏与取消收藏 当入參isCollection == 0 时 进行收藏
	 * 
	 * @param request
	 * @param response
	 * @param user_account
	 * @param ifm_id
	 * @param articleId
	 * @param isCollection
	 * @return
	 */
	@ResponseBody
	@RequestMapping({ "addOrDeleteEnshrine" })
	public String addOrDeleteEnshrine(HttpServletRequest request, HttpServletResponse response, String user_account,
			String ifm_id, String articleId, int isCollection, String title, String moduleId) {
		InformationDistribute distribute = new InformationDistribute();
		ReturnStandardDataFormat StandardData = new ReturnStandardDataFormat(Integer.valueOf(10001), "操作失败", null);
		try {
			if (StringUtils.isNoneBlank(title)) {
				title = EncodingUtils.fixUrlParamEncoding(request, title);
			}
			else{
				//获取方案信息
				distribute = informationDistributeDao.queryContentByArticleId(InformationDistribute.class, articleId,ifm_id);
				title = distribute.getUrl();
			}
			if ((user_account != null) || ("".equals(user_account))) {
				user_account = EncodingUtils.fixUrlParamEncoding(request, user_account);
			}
			if ((ifm_id != null) || ("".equals(ifm_id))) {
				ifm_id = EncodingUtils.fixUrlParamEncoding(request, ifm_id);
			}
			if ((articleId != null) || ("".equals(articleId))) {
				articleId = EncodingUtils.fixUrlParamEncoding(request, articleId);
			}
			if (isCollection == 0) {
				this.enshrineService.addEnshrine(user_account, ifm_id, articleId, new Date());
			} else if (isCollection == 1) {
				this.enshrineService.deleteEnshrine(user_account, null, articleId);
			}
			if (moduleId != null) {
				moduleId = EncodingUtils.fixUrlParamEncoding(request, moduleId);
			}
		} catch (Exception e) {
			addOptRecode(request, optRecordService, "", "", "收藏/取消收藏异常", "根据参数操作异常+模块" + moduleId);
			this.log.error("根据参数操作异常");
			this.log.error(e.getMessage());
			StandardData.setReturnCode(Integer.valueOf(10003));
			StandardData.setReturnMessage("操作异常");
		}
		String categoryName = iformationService.appselectbyifmId(ifm_id);
		String ifmName = iformationService.ifmNamebyifmId(ifm_id);
		if (categoryName == null || categoryName.equals("")) {
			categoryName = "监测";
		}
		// 添加操作日志
		if (isCollection == 0) {
              if(StringUtils.isNotBlank(moduleId)){
			// 1代表监测模块、4推送，2收藏3历史
			if (moduleId.equals("1") || moduleId.equals("4")) {
				addOptRecode(request, optRecordService, OperationModule.monitor.toString(),
						categoryName + "-" + ifmName, OperationDeed.collection.toString() + "《" + title + "》", "");
			} else if (moduleId.equals("2")) {
				addOptRecode(request, optRecordService, OperationModule.me.toString(),
						OperationMinModule.collection.toString(),
						OperationDeed.collection.toString() + "《" + title + "》", "");
			} else if (moduleId.equals("3")) {
				addOptRecode(request, optRecordService, OperationModule.me.toString(),
						OperationMinModule.history.toString(), OperationDeed.collection.toString() + "《" + title + "》",
						"");
			}
              }
		} else if (isCollection == 1) {
			 if(StringUtils.isNotBlank(moduleId)){				 
			if (moduleId.equals("1") || moduleId.equals("4")) {
				addOptRecode(request, optRecordService, OperationModule.monitor.toString(),
						categoryName + "-" + ifmName, OperationDeed.nocollection.toString() + "《" + title + "》", "");
			} else if (moduleId.equals("2")) {
				addOptRecode(request, optRecordService, OperationModule.me.toString(),
						OperationMinModule.collection.toString(),
						OperationDeed.nocollection.toString() + "《" + title + "》", "");
			} else if (moduleId.equals("3")) {
				addOptRecode(request, optRecordService, OperationModule.me.toString(),
						OperationMinModule.history.toString(),
						OperationDeed.nocollection.toString() + "《" + title + "》", "");
			}
		  }
		}
		StandardData.setReturnCode(Integer.valueOf(10000));
		StandardData.setReturnMessage("操作成功");
		return JsonUtils.toString(StandardData, "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 查看用户所拥有的文章
	 * 
	 * @param request
	 * @param response
	 * @param user_account
	 * @return
	 */
	@ResponseBody
	@RequestMapping({ "queryByUserAccount" })
	public String queryByUserAccount(HttpServletRequest request, HttpServletResponse response, String user_account) {
		List<Enshrine> list = new ArrayList<>();
		ReturnStandardDataFormat StandardData = new ReturnStandardDataFormat(Integer.valueOf(10001), "操作失败", list);
		try {
			if ((user_account != null) || ("".equals(user_account))) {
				user_account = EncodingUtils.fixUrlParamEncoding(request, user_account);
			}
			list = this.enshrineService.queryByUserAccount(user_account);
		} catch (Exception e) {
			this.log.error("根据参数查询异常");
			this.log.error(e.getMessage());
			StandardData.setReturnCode(Integer.valueOf(10003));
			StandardData.setReturnMessage("操作异常");
			StandardData.setReturnData(list);
		}
		StandardData.setReturnData(list);
		StandardData.setReturnCode(Integer.valueOf(10000));
		StandardData.setReturnMessage("操作成功");
		return JsonUtils.toString(StandardData, "yyyy-MM-dd HH:mm:ss");
	}
}
